package com.ymatou.iapi.thirdpartysync.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.thirdpartysync.parameter.addProductBean;
import com.ymatou.iapi.thirdpartysync.parameter.fqlSkuInfoList;
import com.ymatou.iapi.thirdpartysync.parameter.getSkuInfoBean;
import com.ymatou.iapi.thirdpartysync.service.getSkuInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.database.model.thirdpartysync.fenqileproduct;
import com.ymttest.database.model.thirdpartysync.fenqileproductExample;
import com.ymttest.database.model.thirdpartysync.fenqilesku;
import com.ymttest.database.model.thirdpartysync.fenqileskuExample;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
/**
*查询分期乐商品信息(交易)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getSkuInfo{
	private static getSkuInfoBean bean;
	private static getSkuInfoCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询分期乐商品信息(交易)");
	}

	@Before
	public void caseUp() {
		bean=new getSkuInfoBean();
		call=new getSkuInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private fenqilesku AddProduct() throws Exception {
		fenqileproductExample productExample=new fenqileproductExample();
		thirdpartysyncWapper syncWapper=new thirdpartysyncWapper(); 
		fenqilesku sku=new fenqilesku();
		fenqileskuExample skuExample=new fenqileskuExample();
		
		addProductBean bean=ThirdpartysyncCallService.initAddProductBean();
		String productId=bean.getProductIdList().get(0);
		
		ThirdpartysyncCallService.AddProductCall(bean,productId);
		
		productExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtSaleStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqileproduct> prodcuts_DB=syncWapper.selectByExample(productExample);
		
		skuExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqilesku> skus_DB=syncWapper.selectByExample(skuExample);
		
		int times=0;
		do{
		 	 if(prodcuts_DB.size()==0 )
			 {
		 	    times+=2000;
				Thread.sleep(times);
				prodcuts_DB=syncWapper.selectByExample(productExample);
				skus_DB=syncWapper.selectByExample(skuExample);
				Logger.comment("查询数据库休眠的时间："+times);
				if(prodcuts_DB.size()==0) continue;
			 }else	 break;
	    }while(times<=10000);

		if(times>10000){
			Logger.comment("商品同步到分期乐异常");
		    return null;
		}
		else {
			 return skus_DB.get(0);
		}
	}
	//校验
	private void check_verify(fenqilesku DB_sku){
		
		Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");
		fqlSkuInfoList return_skus=call.getfenqilesku();
		Logger.verifyEquals(DB_sku.getFqlSkuId(), return_skus.getFqlSkuInfoList().get(0).getFqlSkuId(), "验证fqlSkuId");
		Logger.verifyEquals(DB_sku.getFqlProductId(), return_skus.getFqlSkuInfoList().get(0).getFqlProductId(), "验证FqlProductId");
		Logger.verifyEquals(DB_sku.getYmtProductId(), return_skus.getFqlSkuInfoList().get(0).getYmtProductId(), "验证YmtCatalogId");
		Logger.verifyEquals(DB_sku.getYmtSkuId(), return_skus.getFqlSkuInfoList().get(0).getYmtCatalogId(), "验证YmtCatalogId");
		Logger.verifyEquals(DB_sku.getFqlPrice().stripTrailingZeros().toPlainString(), 
				return_skus.getFqlSkuInfoList().get(0).getFqlPrice().stripTrailingZeros().toPlainString(), "验证FqlPrice");
		Logger.verifyEquals(DB_sku.getYmtSkuId(), return_skus.getFqlSkuInfoList().get(0).getYmtCatalogId(), "验证YmtCatalogId");
		//commission
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getSkuInfo_001() {
		Logger.start(true,"正例-查询分期乐商品信息(交易)");
		try {
			fenqilesku DB_sku=AddProduct();
			List<String> skus=new  ArrayList();
			skus.add(DB_sku.getFqlSkuId());
			
			bean.setFqlSkuIdList(skus);
			call.setData(bean);
			call.callService();			
			//验证
			check_verify(DB_sku);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getSkuInfo_002() {
		Logger.start(true,"反例-查询分期乐商品信息(交易)-fqlSkuIdList中含有不存在的skuId");
		try {
			fenqilesku DB_sku=AddProduct();
			List<String> skus=new  ArrayList();
			skus.add(DB_sku.getFqlSkuId());
			skus.add("1111");
			
			bean.setFqlSkuIdList(skus);
			call.setData(bean);
			call.callService();			
			
			//验证
			Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");
			fqlSkuInfoList return_skus=call.getfenqilesku();
			Logger.verifyEquals(1, return_skus.getFqlSkuInfoList().size(), "验证只返回一条数据");
			check_verify(DB_sku);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getSkuInfo_003() {
		Logger.start(true,"反例-查询分期乐商品信息(交易)-skuId为空");
		try {
			List<String> skus=new  ArrayList();
			
			bean.setFqlSkuIdList(skus);
			call.setData(bean);
			call.callService();					
			Logger.verifyEquals("false", call.getSuccess(), "验证success返回true");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getSkuInfo_004() {
		Logger.start(true,"反例-查询分期乐商品信息(交易)-skuId不存在");
		try {
			List<String> skus=new  ArrayList();
			skus.add("1111");
			
			bean.setFqlSkuIdList(skus);
			call.setData(bean);
			call.callService();			
			
			//验证
			Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");
			fqlSkuInfoList return_skus=call.getfenqilesku();
			Logger.verifyEquals(0, return_skus.getFqlSkuInfoList().size(), "验证只返回一条数据");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}